from rest_framework.response import Response

from apps.HttpTestcase.models import TestcaseReports,TestsuiteTestcaseReports
from apps.HttpTestcase.models import TestsuiteReports
from rest_framework.views import APIView
from django.db.models import Sum
from apps.HttpTestcase.serializers import ReportsSerializer, TestsuiteReportsSerializer, TestsuiteTestcaseReportsSerializer
from utils.response import SuccessResponse


class TestcaseReportsDetails(APIView):
    """
    用例报告
    """
    def get(self, request, *args, **kwargs):
        # 版本号
        version = request.query_params.get('version')
        status = request.query_params.get('status', None)
        if version:
            testcase_results = TestcaseReports.objects.filter(version=version, is_delete=False)
            testcase_count = testcase_results.count()
            success = testcase_results.filter(status='PASS').count()
            fail = testcase_results.filter(status='FAIL').count()
            run_time = testcase_results.aggregate(Sum('elapsed_ms'))['elapsed_ms__sum']

            success_rate = round(((success / testcase_count) * 100), 2)

            if status:
                testcase_results = testcase_results.filter(status='status')

            data = []
            for i in testcase_results:
                testcase_result = ReportsSerializer(i).data
                data.append(testcase_result)
            response = {
                'testcase_count': testcase_count,
                'success': success,
                'fail': fail,
                'success_rate': success_rate,
                'run_time': run_time,
                'results': data
            }

            return SuccessResponse(data=response, msg="OK",)


class TestsuiteReportsAPIView(APIView):
    def get(self, request, *args, **kwargs):
        version = request.query_params.get('version')
        # status = request.query_params.get('status', None)
        if version:
            testsuite_testcases_reports = TestsuiteTestcaseReports.objects.filter(version=version, is_delete=False)
            testcase_count = testsuite_testcases_reports.count()
            success = testsuite_testcases_reports.filter(status='PASS').count()
            fail = testsuite_testcases_reports.filter(status='FAIL').count()

            run_time = testsuite_testcases_reports.aggregate(Sum('elapsed_ms'))['elapsed_ms__sum']

            success_rate = round(((success / testcase_count) * 100), 2)

            testsuite_reports = TestsuiteReports.objects.filter(version=version, is_delete=False)

            data = []
            for i in testsuite_reports:
                testsuite_serializer = TestsuiteReportsSerializer(i).data
                testsuite_reports_id = testsuite_serializer.get('id')
                testsuite_count = testsuite_testcases_reports.filter(testsuite_reports_id=testsuite_reports_id, is_delete=False).count()
                testsuite_success = testsuite_testcases_reports.filter(status='PASS', is_delete=False).count()
                testsuite_fail = testsuite_testcases_reports.filter(status='FAIL', is_delete=False).count()
                testsute_rate = round(((testsuite_success / testsuite_count) * 100), 2)
                testsuite_serializer['count'] = testsuite_count
                testsuite_serializer['success'] = testsuite_success
                testsuite_serializer['fail'] = testsuite_fail
                testsuite_serializer['rate'] = testsute_rate
                testsuite_serializer['testcase_data'] = []

                testcases_res = testsuite_testcases_reports.filter(testsuite_reports_id=testsuite_reports_id)
                for testcase_res in testcases_res:
                    testsuite_serializer['testcase_data'].append(TestsuiteTestcaseReportsSerializer(testcase_res).data)
                data.append(testsuite_serializer)

            response = {
                'count': testcase_count,
                'success': success,
                'fail': fail,
                'success_rate': success_rate,
                'run_time': run_time,
                'data': data
            }

            return SuccessResponse(data=response, msg="OK",)
